-- Graeme, the create Index statement was executed using the following syntax. CREATE INDEX sidx_sensor_report_y2014m09 ON doti_sensor_report_y2014m09 USING GIST (sensor_location) TABLESPACE doti_data_y2014; *** Leveraging the spatial index *** -- 1.) Dropped index "idx_sensor_report_query_y2014m09" on the doti_sensor_report_y2014m09 and toggled the spatial index to a valid state to force planner to use it to execute query. -- i.e. update pg_index set indisvalid = true where indexrelid = 'sidx_sensor_report_y2014m09'::regclass; -- 2.) Executed query plan. This was our original configuration that was taking over 10 minutes to return results to the UI. Today the runtimes are slightly better than the compound index "idx_sensor_report_query_y2014m09" explain (analyze,buffers) select count(*) as y0_ from DOTI_SENSOR_REPORT this_ where this_.node_date_time between '2014-09-20 23:40:56.245'::timestamp without time zone and '2014-09-21 07:36:47.388'::timestamp without time zone and this_.model_uid=20164 and (ST_within (this_.sensor_location,'010300000001000000050000006787D1E89889F5BFFBA6BE01196EE53F1AF703F9588EF5BF6D9AC3A07D5FE53F0C2792E0B193F5BF6D9AC3A07D5FE53FC096C4F07198F5BFFBA6BE01196EE53F6787D1E89889F5BFFBA6BE01196EE53F') or ST_touches (this_.sensor_location,'010300000001000000050000006787D1E89889F5BFFBA6BE01196EE53F1AF703F9588EF5BF6D9AC3A07D5FE53F0C2792E0B193F5BF6D9AC3A07D5FE53FC096C4F07198F5BFFBA6BE01196EE53F6787D1E89889F5BFFBA6BE01196EE53F')) QUERY PLAN --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Aggregate (cost=12848201.03..12848201.06 rows=1 width=0) (actual time=282105.704..282105.705 rows=1 loops=1) Buffers: shared hit=284476 read=204819 -> Append (cost=0.00..12848200.47 rows=225 width=0) (actual time=282105.700..282105.700 rows=0 loops=1) Buffers: shared hit=284476 read=204819 -> Seq Scan on doti_sensor_report this_ (cost=0.00..0.00 rows=1 width=0) (actual time=0.001..0.001 rows=0 loops=1) Filter: ((node_date_time >= '2014-09-20 23:40:56.245'::timestamp without time zone) AND (node_date_time <= '2014-09-21 07:36:47.388'::timestamp without time zone) AND (sensor_location && '010300000001000000050000006787D1E89889F5BFFBA6BE01196EE53F1AF703F9588EF5BF6D9AC3A07D5FE53F0C2792E0B193F5BF6D9AC3A07D5FE53FC096C4F07198F5BFFBA6BE01196EE53F6787D1E89889F5BFFBA6BE01196EE53F'::geometry) AND (model_uid = 20164) AND (_st_contains('010300000001000000050000006787D1E89889F5BFFBA6BE01196EE53F1AF703F9588EF5BF6D9AC3A07D5FE53F0C2792E0B193F5BF6D9AC3A07D5FE53FC096C4F07198F5BFFBA6BE01196EE53F6787D1E89889F5BFFBA6BE01196EE53F'::geometry, sensor_location) OR _st_touches(sensor_location, '010300000001000000050000006787D1E89889F5BFFBA6BE01196EE53F1AF703F9588EF5BF6D9AC3A07D5FE53F0C2792E0B193F5BF6D9AC3A07D5FE53FC096C4F07198F5BFFBA6BE01196EE53F6787D1E89889F5BFFBA6BE01196EE53F'::geometry))) -> Bitmap Heap Scan on doti_sensor_report_y2014m09 this__1 (cost=300474.83..12848125.67 rows=223 width=0) (actual time=282105.694..282105.694 rows=0 loops=1) Recheck Cond: (sensor_location && '010300000001000000050000006787D1E89889F5BFFBA6BE01196EE53F1AF703F9588EF5BF6D9AC3A07D5FE53F0C2792E0B193F5BF6D9AC3A07D5FE53FC096C4F07198F5BFFBA6BE01196EE53F6787D1E89889F5BFFBA6BE01196EE53F'::geometry) Rows Removed by Index Recheck: 2521326 Filter: ((node_date_time >= '2014-09-20 23:40:56.245'::timestamp without time zone) AND (node_date_time <= '2014-09-21 07:36:47.388'::timestamp without time zone) AND (model_uid = 20164) AND (_st_contains('010300000001000000050000006787D1E89889F5BFFBA6BE01196EE53F1AF703F9588EF5BF6D9AC3A07D5FE53F0C2792E0B193F5BF6D9AC3A07D5FE53FC096C4F07198F5BFFBA6BE01196EE53F6787D1E89889F5BFFBA6BE01196EE53F'::geometry, sensor_location) OR _st_touches(sensor_location, '010300000001000000050000006787D1E89889F5BFFBA6BE01196EE53F1AF703F9588EF5BF6D9AC3A07D5FE53F0C2792E0B193F5BF6D9AC3A07D5FE53FC096C4F07198F5BFFBA6BE01196EE53F6787D1E89889F5BFFBA6BE01196EE53F'::geometry))) Rows Removed by Filter: 14867114 Buffers: shared hit=284476 read=204819 -> Bitmap Index Scan on sidx_sensor_report_y2014m09 on doti_sensor_report_y2014m09 (cost=0.00..300474.77 rows=14742696 width=0) (actual time=196575.084..196575.084 rows=14867114 loops=1) Index Cond: (sensor_location && '010300000001000000050000006787D1E89889F5BFFBA6BE01196EE53F1AF703F9588EF5BF6D9AC3A07D5FE53F0C2792E0B193F5BF6D9AC3A07D5FE53FC096C4F07198F5BFFBA6BE01196EE53F6787D1E89889F5BFFBA6BE01196EE53F'::geometry) Buffers: shared hit=16887 read=82607 -> Seq Scan on doti_sensor_report_overflow this__2 (cost=0.00..74.80 rows=1 width=0) (actual time=0.002..0.002 rows=0 loops=1) Filter: ((node_date_time >= '2014-09-20 23:40:56.245'::timestamp without time zone) AND (node_date_time <= '2014-09-21 07:36:47.388'::timestamp without time zone) AND (sensor_location && '010300000001000000050000006787D1E89889F5BFFBA6BE01196EE53F1AF703F9588EF5BF6D9AC3A07D5FE53F0C2792E0B193F5BF6D9AC3A07D5FE53FC096C4F07198F5BFFBA6BE01196EE53F6787D1E89889F5BFFBA6BE01196EE53F'::geometry) AND (model_uid = 20164) AND (_st_contains('010300000001000000050000006787D1E89889F5BFFBA6BE01196EE53F1AF703F9588EF5BF6D9AC3A07D5FE53F0C2792E0B193F5BF6D9AC3A07D5FE53FC096C4F07198F5BFFBA6BE01196EE53F6787D1E89889F5BFFBA6BE01196EE53F'::geometry, sensor_location) OR _st_touches(sensor_location, '010300000001000000050000006787D1E89889F5BFFBA6BE01196EE53F1AF703F9588EF5BF6D9AC3A07D5FE53F0C2792E0B193F5BF6D9AC3A07D5FE53FC096C4F07198F5BFFBA6BE01196EE53F6787D1E89889F5BFFBA6BE01196EE53F'::geometry))) Total runtime: 282108.715 ms <- 4.7018 minutes - unacceptable to User community (18 rows) *** Updated table Statistics *** marsdb=# select * from pg_stat_user_tables where relname = 'doti_sensor_report_y2014m09'; relid | schemaname | relname | seq_scan | seq_tup_read | idx_scan | idx_tup_fetch | n_tup_ins | n_tup_upd | n_tup_del | n_tup_hot_upd | n_live_tup | n_dead_tup | last_vacuum | last_autovacuum | last_analyze | last_autoanalyze | vacuum_count | autovacuum_count | analyze_count | autoanalyze_count ---------+------------+-----------------------------+----------+--------------+----------+---------------+-----------+-----------+-----------+---------------+------------+------------+-------------+---------------------------------+-------------------------------+-------------------------------+--------------+------------------+---------------+------------------- 1820279 | public | doti_sensor_report_y2014m09 | 20 | 3226221906 | 22770 | 38741010306 | 210470837 | 0 | 1676570 | 0 | 230047224 | 1602995 | | 2014-09-22 10:25:17.095126+00 | 2014-09-26 21:06:17.817324+00 | 2014-09-26 04:37:52.687816+00 | 0 | 2 | 2 | 5 (1 row) -- re-ran explain plan leveraging updated statistics explain (analyze,buffers) select count(*) as y0_ from DOTI_SENSOR_REPORT this_ where this_.node_date_time between '2014-09-20 23:40:56.245'::timestamp without time zone and '2014-09-21 07:36:47.388'::timestamp without time zone and this_.model_uid=20164 and (ST_within (this_.sensor_location,'010300000001000000050000006787D1E89889F5BFFBA6BE01196EE53F1AF703F9588EF5BF6D9AC3A07D5FE53F0C2792E0B193F5BF6D9AC3A07D5FE53FC096C4F07198F5BFFBA6BE01196EE53F6787D1E89889F5BFFBA6BE01196EE53F') or ST_touches (this_.sensor_location,'010300000001000000050000006787D1E89889F5BFFBA6BE01196EE53F1AF703F9588EF5BF6D9AC3A07D5FE53F0C2792E0B193F5BF6D9AC3A07D5FE53FC096C4F07198F5BFFBA6BE01196EE53F6787D1E89889F5BFFBA6BE01196EE53F')) QUERY PLAN --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Aggregate (cost=112542.35..112542.38 rows=1 width=0) (actual time=347664.232..347664.232 rows=1 loops=1) Buffers: shared hit=157038 -> Append (cost=0.00..137572.97 rows=3885 width=0) (actual time=168.419..348986.845 rows=443542 loops=1) Buffers: shared hit=157035 -> Seq Scan on doti_sensor_report this_ (cost=0.00..0.00 rows=1 width=0) (actual time=0.001..0.001 rows=0 loops=1) Filter: ((node_date_time >= '2014-09-20 23:40:56.245'::timestamp without time zone) AND (node_date_time <= '2014-09-21 07:36:47.388'::timestamp without time zone) AND (sensor_location && '010300000001000000050000006787D1E89889F5BFFBA6BE01196EE53F1AF703F9588EF5BF6D9AC3A07D5FE53F0C2792E0B193F5BF6D9AC3A07D5FE53FC096C4F07198F5BFFBA6BE01196EE53F6787D1E89889F5BFFBA6BE01196EE53F'::geometry) AND (model_uid = 20164) AND (_st_contains('010300000001000000050000006787D1E89889F5BFFBA6BE01196EE53F1AF703F9588EF5BF6D9AC3A07D5FE53F0C2792E0B193F5BF6D9AC3A07D5FE53FC096C4F07198F5BFFBA6BE01196EE53F6787D1E89889F5BFFBA6BE01196EE53F'::geometry, sensor_location) OR _st_touches(sensor_location, '010300000001000000050000006787D1E89889F5BFFBA6BE01196EE53F1AF703F9588EF5BF6D9AC3A07D5FE53F0C2792E0B193F5BF6D9AC3A07D5FE53FC096C4F07198F5BFFBA6BE01196EE53F6787D1E89889F5BFFBA6BE01196EE53F'::geometry))) -> Index Scan using idx_sensor_report_query_y2014m09 on doti_sensor_report_y2014m09 this__1 (cost=0.57..112460.53 rows=2806 width=0) (actual time=150.115..347438.135 rows=443542 loops=1) Index Cond: ((model_uid = 20164) AND (node_date_time >= '2014-09-20 23:40:56.245'::timestamp without time zone) AND (node_date_time <= '2014-09-21 07:36:47.388'::timestamp without time zone)) Filter: ((sensor_location && '010300000001000000050000006787D1E89889F5BFFBA6BE01196EE53F1AF703F9588EF5BF6D9AC3A07D5FE53F0C2792E0B193F5BF6D9AC3A07D5FE53FC096C4F07198F5BFFBA6BE01196EE53F6787D1E89889F5BFFBA6BE01196EE53F'::geometry) AND (_st_contains('010300000001000000050000006787D1E89889F5BFFBA6BE01196EE53F1AF703F9588EF5BF6D9AC3A07D5FE53F0C2792E0B193F5BF6D9AC3A07D5FE53FC096C4F07198F5BFFBA6BE01196EE53F6787D1E89889F5BFFBA6BE01196EE53F'::geometry, sensor_location) OR _st_touches(sensor_location, '010300000001000000050000006787D1E89889F5BFFBA6BE01196EE53F1AF703F9588EF5BF6D9AC3A07D5FE53F0C2792E0B193F5BF6D9AC3A07D5FE53FC096C4F07198F5BFFBA6BE01196EE53F6787D1E89889F5BFFBA6BE01196EE53F'::geometry))) Rows Removed by Filter: 3310409 Buffers: shared hit=157035 -> Seq Scan on doti_sensor_report_overflow this__2 (cost=0.00..74.80 rows=1 width=0) (actual time=0.002..0.002 rows=0 loops=1) Filter: ((node_date_time >= '2014-09-20 23:40:56.245'::timestamp without time zone) AND (node_date_time <= '2014-09-21 07:36:47.388'::timestamp without time zone) AND (sensor_location && '010300000001000000050000006787D1E89889F5BFFBA6BE01196EE53F1AF703F9588EF5BF6D9AC3A07D5FE53F0C2792E0B193F5BF6D9AC3A07D5FE53FC096C4F07198F5BFFBA6BE01196EE53F6787D1E89889F5BFFBA6BE01196EE53F'::geometry) AND (model_uid = 20164) AND (_st_contains('010300000001000000050000006787D1E89889F5BFFBA6BE01196EE53F1AF703F9588EF5BF6D9AC3A07D5FE53F0C2792E0B193F5BF6D9AC3A07D5FE53FC096C4F07198F5BFFBA6BE01196EE53F6787D1E89889F5BFFBA6BE01196EE53F'::geometry, sensor_location) OR _st_touches(sensor_location, '010300000001000000050000006787D1E89889F5BFFBA6BE01196EE53F1AF703F9588EF5BF6D9AC3A07D5FE53F0C2792E0B193F5BF6D9AC3A07D5FE53FC096C4F07198F5BFFBA6BE01196EE53F6787D1E89889F5BFFBA6BE01196EE53F'::geometry))) Total runtime: 349105.317 ms <-- last run: 5.81842 minutes - unacceptable to User community Current runtime: 347644.312 ms <-- 5.79407 minutes - still slow - unacceptable to User community (14 rows) *** One very promising development *** Graeme, mentioned using the St_Intersets clause. So I ran some tests using this re-factored query. Counts are returning in milliseconds using this method. Can there be descrepancies using this method instead. Results are coming back with the identical row counts thus far in my testing? explain (analyze,buffers) select count(*) as y0_ from DOTI_SENSOR_REPORT this_ where this_.node_date_time between '2014-09-20 23:40:56.245'::timestamp without time zone and '2014-09-21 07:36:47.388'::timestamp without time zone and this_.model_uid=20164 and ST_Intersets(this_.sensor_location,'010300000001000000050000006787D1E89889F5BFFBA6BE01196EE53F1AF703F9588EF5BF6D9AC3A07D5FE53F0C2792E0B193F5BF6D9AC3A07D5FE53FC096C4F07198F5BFFBA6BE01196EE53F6787D1E89889F5BFFBA6BE01196EE53F'); thanks